<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="third/zTree_v3/css/demo.css" type="text/css">
<link rel="stylesheet" href="third/zTree_v3/css/zTreeStyle/zTreeStyle.css"
	type="text/css">
<script type="text/javascript" src="third/zTree_v3/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="third/zTree_v3/js/jquery.ztree.core.js"></script>
<script type="text/javascript" src="third/zTree_v3/js/jquery.ztree.excheck.js"></script>
<script type="text/javascript" src="third/zTree_v3/js/jquery.ztree.exedit.js"></script>
<script type="text/javascript">
	var setting = {
// 		async : {
// 			enable : true,//开启异步加载处理
// 			url : encodeURI(encodeURI("/peTreeDemo2/jsondata")),
// 			autoParam : [ "id" ],
// 			dataFilter : filter,
// 			contentType : "application/json",
// 			type : "get"
// 		},
		view : {
			expandSpeed : "",
			addHoverDom : addHoverDom,
			removeHoverDom : removeHoverDom,
			selectedMulti : false
		},
		edit : {
			enable : true
		},
		data : {
			simpleData : {
				enable : true
			}
		},
		callback : {
			beforeRemove : beforeRemove,
			beforeRename : beforeRename,
		}
	};
	function filter(treeId, parentNode, childNodes) {
		if (!childNodes)
			return null;
		for (var i = 0, l = childNodes.length; i < l; i++) {
			childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
		}
		return childNodes;
	}
	function beforeRemove(treeId, treeNode) {
		if (confirm("确认删除节点--" + treeNode.name + "--吗?")) {
			var param = "id=" + treeNode.id;
			$.post(encodeURI(encodeURI("/peTreeDemo2/jsondata?method=deletePp&"
					+ param)));
		} else {
			return false;
		}
	}
	function beforeRename(treeId, treeNode, newName) {
		if (newName.length == 0) {
			alert("节点名称不能为空.");
			return false;
		}
		var param = "id=" + treeNode.id + "&name=" + newName;
		$.post(encodeURI(encodeURI("/peTreeDemo2/jsondata?method=updatePp&"
				+ param)));
		return true;
	}
 
	function addHoverDom(treeId, treeNode) {
		var sObj = $("#" + treeNode.tId + "_span");
		if (treeNode.editNameFlag || $("#addBtn_" + treeNode.tId).length > 0)
			return;
		var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
				+ "' title='add node' οnfοcus='this.blur();'></span>";
		sObj.after(addStr);
		var btn = $("#addBtn_" + treeNode.tId);
		if (btn)
			btn.bind("click", function() {
				var Ppname = prompt("请输入新节点名称");
				if (Ppname == null) {
					return;
				} else if (Ppname == "") {
					alert("节点名称不能为空");
				} else {
					var param ="&pId="+ treeNode.id + "&name=" + Ppname;
					var zTree = $.fn.zTree.getZTreeObj("treeDemo");
					$.post(
							encodeURI(encodeURI("/peTreeDemo2/jsondata?method=addPp&"
									+ param)), function(data) {
								if ($.trim(data) != null) {
									var treenode = $.trim(data);
									zTree.addNodes(treeNode, {
										pId : treeNode.id,
										name : Ppname
									}, true);
								}
							})
				}
 
			});
	};
	function removeHoverDom(treeId, treeNode) {
		$("#addBtn_" + treeNode.tId).unbind().remove();
	};
	


	//树的数据对象集
	var zNodes = null;

	$(function(){
		$.ajax({
		    url: "http://localhost:8086/medical/RoleServlet",
// 		    contentType: "application/json",
		    type: "post",
		    dataType:'json',
		    async: false,
		    success: function (result) {
		        // list是后端已经格式好的数据列表
		        zNodes = result;
// 		        zNodes = JSON.stringify(result);
// 		        console.log("result" + reslut);
		    }
		});
	});


	$(document).ready(function() {
		$.fn.zTree.init($("#treeDemo"), setting, zNodes);
 
	});

	 <%-- var setting = {
             isSimpleData : true, //数据是否采用简单 Array 格式，默认false
             treeNodeKey : "id", //在isSimpleData格式下，当前节点id属性
             treeNodeParentKey : "pId", //在isSimpleData格式下，当前节点的父节点id属性
             showLine : true, //是否显示节点间的连线
               async:{--  zTree官方文档说明  异步加载必须写上这个参数  但是我这里没写T-T 一样的可以 不知道我是不是假的异步加载 本人在这里也有疑问 %>  
<%--                    enable: true,
                   url: "<%=basePath%>department/testYbTree2.do",
                   autoParam: ["id"]
               },
             callback :{
                 onClick : function(event, treeId, treeNode, clickFlag) {  
                     // 判断是否父节点  
                     if(!treeNode.isParent){  
                         //alert("treeId自动编号：" + treeNode.tId + ", 节点id是：" + treeNode.id + ", 节点文本是：" + treeNode.name);  
                         $.ajax({
                             url: "RoleServlet?m=add",//请求的action路径
                             data:{"pid":treeNode.id},
                             error: function () {//请求失败处理函数
                                 alert('请求失败');
                             },
                             success:function(data)
                                 { //添加子节点到指定的父节点
                                     var jsondata= eval(data);
                                     if(jsondata == null || jsondata == ""){
                                         //末节点的数据为空   所以不再添加节点  这里可以根据业务需求自己写
                                         //$("#treeFrame").attr("src",treeNode.url);
                                         }
                                     else{
                                             var treeObj = $.fn.zTree.getZTreeObj("demotree");
                                             //treeNode.halfCheck = false;
                                             var parentZNode = treeObj.getNodeByParam("id", treeNode.id, null);//获取指定父节点
                                             newNode = treeObj.addNodes(parentZNode,jsondata, false);
                                         }
                                 }
                             });
                     } 
                 }
             },
             //checkable : true //每个节点上是否显示 CheckBox
             }; --%>


	//树属性的定义
	/* var setting = {
	    //页面上的显示效果
	    view: {
	        addHoverDom: addHoverDom, //当鼠标移动到节点上时，显示用户自定义控件
	        removeHoverDom: removeHoverDom, //离开节点时的操作
// 	        fontCss: getFontCss //个性化样式
	    },
	    edit: {
	        enable: true, //单独设置为true时，可加载修改、删除图标
	        editNameSelectAll: true,
// 	        showRemoveBtn: showRemoveBtn,
// 	        showRenameBtn: showRenameBtn
	    },
	    data: {
	        simpleData: {
	            enable:true,
	            idKey: "id",
	            pIdKey: "pId",
	            system:"system",
	            rootPId: ""
	        }
	    },
	    callback: {
	    	beforeRemove : beforeRemove,
			beforeRename : beforeRename,
// 	        onClick: zTreeOnClick, //单击事件
// 	        onRemove: onRemove, //移除事件
// 	        onRename: onRename //修改事件
	    }
	}; */
	


	
	 /* var setting = {
// 		async : {
// 			enable : true,//开启异步加载处理
// 			url : "RoleServlet",
// // 			url : encodeURI(encodeURI("RoleServlet")),
// 			autoParam : [ "id" ],
// 			dataFilter : filter,
// 			contentType : "application/json",
// 			type : "get"
// 		},
		view : {
			expandSpeed : "",
			addHoverDom : addHoverDom,
			removeHoverDom : removeHoverDom,
			selectedMulti : false
		},
		edit : {
			enable : true
		},
		data : {
			simpleData : {
				enable : true
			}
		},
		callback : {
			beforeRemove : beforeRemove,
			beforeRename : beforeRename,
		}
	}; */
	function filter(treeId, parentNode, childNodes) {
		if (!childNodes)
			return null;
		for (var i = 0, l = childNodes.length; i < l; i++) {
			childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
		}
		return childNodes;
	}
	function beforeRemove(treeId, treeNode) {
		if (confirm("确认删除节点--" + treeNode.name + "--吗?")) {
			var param = "id=" + treeNode.id;
			$.post(encodeURI(encodeURI("RoleServlet?m=deletePp&"
					+ param)));
		} else {
			return false;
		}
	}
	function beforeRename(treeId, treeNode, newName) {
		if (newName.length == 0) {
			alert("节点名称不能为空.");
			return false;
		}
		var param = "id=" + treeNode.id + "&name=" + newName;
		$.post(encodeURI(encodeURI("RoleServlet?m=updatePp&"
				+ param)));
		return true;
	}
 
	function addHoverDom(treeId, treeNode) {
		var sObj = $("#" + treeNode.tId + "_span");
		if (treeNode.editNameFlag || $("#addBtn_" + treeNode.tId).length > 0)
			return;
		var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
				+ "' title='add node' οnfοcus='this.blur();'></span>";
		sObj.after(addStr);
		var btn = $("#addBtn_" + treeNode.tId);
		if (btn)
			btn.bind("click", function() {
				var Ppname = prompt("请输入新节点名称");
				if (Ppname == null) {
					return;
				} else if (Ppname == "") {
					alert("节点名称不能为空");
				} else {
					var param ="&pId="+ treeNode.id + "&name=" + Ppname;
					var zTree = $.fn.zTree.getZTreeObj("treeDemo");
					$.post(
							encodeURI(encodeURI("RoleServlet?m=addPp&"
									+ param)), function(data) {
								if ($.trim(data) != null) {
									var treenode = $.trim(data);
									zTree.addNodes(treeNode, {
										pId : treeNode.id,
										name : Ppname
									}, true);
								}
							})
				}
 
			});
	};
	function removeHoverDom(treeId, treeNode) {
		$("#addBtn_" + treeNode.tId).unbind().remove();
	};
	$(document).ready(function() {
		console.log("data>>>>" + JSON.stringify(zNodes));
		
		$.fn.zTree.init($("#treeDemo"), setting, zNodes);
 
	});
</script>
</head>
<body>
	<div style="width:35%;">
        <ul id="demotree" class="ztree"></ul>
    </div>
</body>
</html>